package com.qigao.mall.web.controller.app.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.OrderService;
import com.qigao.mall.api.ShopEmpService;
import com.qigao.mall.commons.dto.OrderModiDto;
import com.qigao.mall.commons.dto.OrderQueryDto;
import com.qigao.mall.commons.dto.OrderResultDto;
import com.qigao.mall.commons.enums.OrderActionEnum;
import com.qigao.mall.commons.enums.OrderStatusEnum;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.web.controller.AbstractOrderController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.OrderConfirmSendVo;
import com.qigao.mall.web.vo.app.OrderModiVo;
import com.qigao.mall.web.vo.app.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@RestController
@RequestMapping("/app/merchant/order/")
@Api(tags = "App商户操作订单相关API" ,value = "App商户操作订单相关API")
public class MerchantOrderController extends AbstractOrderController{

    private static Logger logger= LoggerFactory.getLogger(MerchantOrderController.class);

    @Resource
    private OrderService orderService;
    @Resource
    private ShopEmpService shopEmpService;

    //3.按照订单号短码查询某个店铺下的订单信息
    @PostMapping("/findOrderByRecv/{shopNo}/{orderRecv}")
    @ApiOperation(value = "按照订单号短码查询某个店铺下的订单信息", notes = "按照订单号短码查询某个店铺下的订单信息")
    public Result<OrderResultDto> findOrderByRecv(@PathVariable("shopNo")String shopNo,@PathVariable("orderRecv")String orderRecv,HttpServletRequest request){
        try{
            //参数校验
            return orderService.findShopOrderByRecv(orderRecv,shopNo);
        }catch (Exception e){
            logger.error("[按照订单号短码查询某个店铺下的订单信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    //1.按照订单号更新订单信息 商户修改订单备注/商户修改订单价格
    @PostMapping("/updateMerOrder")
    @ApiOperation(value = "按照订单号更新订单信息", notes = "按照订单号更新订单信息")
    public Result<String> updateMerOrder(@RequestBody OrderModiVo orderModiVo, HttpServletRequest request){
        //参数校验
        ValidatorUtil.validateEntity(orderModiVo,OrderModiVo.class);
        try{
            OrderModiDto orderModiDto=new OrderModiDto();
            orderModiDto.setShopNo(orderModiVo.getShopNo());
            orderModiDto.setOrderNo(orderModiVo.getOrderNo());
            orderModiDto.setMerMemo(orderModiVo.getMerMemo());
            orderModiDto.setActOrderAmt(new BigDecimal(orderModiVo.getActOrderAmt()));
            orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            orderService.updateMerOrder(orderModiDto);
            return Result.newSuccess();
        }catch (BusinessException be){
            logger.error("[按照订单号更新订单信息]发生业务异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        } catch(Exception e){
            logger.error("[按照订单号更新订单信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @PostMapping("/confirmMoney")
    @ApiOperation(value = "确认收钱", notes = "确认收钱")
    public Result<String> confirmMoney(@RequestBody OrderConfirmSendVo orderConfirmSendVo, HttpServletRequest request){
        //参数校验
        ValidatorUtil.validateEntity(orderConfirmSendVo,OrderConfirmSendVo.class);
        boolean ifExistsShopEmp=shopEmpService.ifShopEmp(orderConfirmSendVo.getShopNo(),RequestUtils.getCurrentUserNo(request));
        if(!ifExistsShopEmp){
            logger.error("非本店铺店员，不能操作订单{}",orderConfirmSendVo);
            return Result.newError(ResultCode.OPER_NOT_ALlOWED);
        }
        try{
            OrderModiDto orderModiDto=new OrderModiDto();
            orderModiDto.setShopNo(orderConfirmSendVo.getShopNo());
            orderModiDto.setOrderAction(OrderActionEnum.CONFIRM_MONEY);
            orderModiDto.setOrderNo(orderConfirmSendVo.getOrderNo());
            orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            orderModiDto.setOrderStatus(OrderStatusEnum.HAS_PAY.getCode());
            return orderService.updateOrderStatus(orderModiDto);
        }catch (Exception e){
            logger.error("[确认发货]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @PostMapping("/confirmSend")
    @ApiOperation(value = "确认发货", notes = "确认发货")
    public Result<String> confirmSend(@RequestBody OrderConfirmSendVo orderConfirmSendVo, HttpServletRequest request){
        //参数校验
        ValidatorUtil.validateEntity(orderConfirmSendVo,OrderConfirmSendVo.class);
        boolean ifExistsShopEmp=shopEmpService.ifShopEmp(orderConfirmSendVo.getShopNo(),RequestUtils.getCurrentUserNo(request));
        if(!ifExistsShopEmp){
            logger.error("非本店铺店员，不能操作订单{}",orderConfirmSendVo);
            return Result.newError(ResultCode.OPER_NOT_ALlOWED);
        }
        try{
            OrderModiDto orderModiDto=new OrderModiDto();
            orderModiDto.setShopNo(orderConfirmSendVo.getShopNo());
            orderModiDto.setOrderAction(OrderActionEnum.CONFIRM_SEND);
            orderModiDto.setOrderNo(orderConfirmSendVo.getOrderNo());
            orderModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            orderModiDto.setOrderStatus(OrderStatusEnum.HAS_SEND.getCode());
            return orderService.updateOrderStatus(orderModiDto);
        }catch (Exception e){
            logger.error("[确认发货]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //2.订单查询列表
    @PostMapping("/findAllOrder")
    @ApiOperation(value = "获取所有订单列表", notes = "获取所有订单列表")
    public Result<PageInfo<OrderResultDto>> findAllOrder(@RequestBody OrderQueryVo orderQueryVo, HttpServletRequest request){
        try{
            //参数校验
            OrderQueryDto orderQueryDto=new OrderQueryDto();
            BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
            return  orderService.findOrderByCond(orderQueryDto);
        }catch (Exception e){
            logger.error("[订单查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //2.按照订单状态查询订单列表
    @PostMapping("/findOrderByStatus")
    @ApiOperation(value = "按照订单状态查询所有用户订单列表", notes = "按照订单状态查询所有用户订单列表")
    public Result<PageInfo<OrderResultDto>> findOrderByStatus(@RequestBody OrderQueryVo orderQueryVo, HttpServletRequest request){
        try{
            //参数校验
            if(StringUtils.isEmpty(orderQueryVo.getOrderStatus())){
                logger.error("[按照订单状态查询所有用户订单列表]订单状态未传递");
                return Result.newError(ResultCode.COMMON_PARAM_INVALID);
            }
            OrderQueryDto orderQueryDto=new OrderQueryDto();
            BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
            orderQueryDto.setOrderStatus(Integer.valueOf(orderQueryVo.getOrderStatus()));
            return  orderService.findOrderByCond(orderQueryDto);
        }catch (Exception e){
            logger.error("[按照订单状态查询所有用户订单列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //3.按照用户编号获取订单列表
    @PostMapping("/findOrderByUserNo")
    @ApiOperation(value = "按照用户编号获取订单列表", notes = "按照用户编号获取订单列表")
    public Result<PageInfo<OrderResultDto>> findOrderByUserNo(@RequestBody OrderQueryVo orderQueryVo, HttpServletRequest request){
        try{
            //参数校验
            if(StringUtils.isEmpty(orderQueryVo.getUserNo())){
                logger.error("[按照用户编号获取订单列表]用户号未传递");
                return Result.newError(ResultCode.COMMON_PARAM_INVALID);
            }
            OrderQueryDto orderQueryDto=new OrderQueryDto();
            BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
            orderQueryDto.setUserNo(orderQueryVo.getUserNo());
            return  orderService.findOrderByCond(orderQueryDto);
        }catch (Exception e){
            logger.error("[按照用户编号获取订单列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    //2.按照日期倒序显示已付款订单信息
    @PostMapping("/findAllPayOrderList")
    @ApiOperation(value = "按照日期倒序显示已付款订单信息", notes = "按照日期倒序显示已付款订单信息，需要传入展示页数")
    public Result<PageInfo<OrderResultDto>> findAllPayOrderList(@RequestBody OrderQueryVo orderQueryVo){
        try{
            //参数校验
            OrderQueryDto orderQueryDto=new OrderQueryDto();
            BeanUtils.copyProperties(orderQueryVo,orderQueryDto);
            return  orderService.findAllPayOrderList(orderQueryDto);
        }catch (Exception e){
            logger.error("[按照日期倒序显示已付款订单信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }




}
